package com.rcp.login;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LogoutServlet
 */
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	private String ClockOut;
	private static final String DATE_FORMAT_NOW = "MM/dd/yyyy HH:mm:ss";
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LogoutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				
				Calendar cal = Calendar.getInstance();
				SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
	
				//Get reference to session and invalidate
				HttpSession session = request.getSession();
	
				ClockOut = sdf.format(cal.getTime());
	
				String eID = (String)session.getAttribute("EmployeeID");
				String cI = (String)session.getAttribute("ClockIn");
	
				// Define connection variables
				String className = "com.mysql.jdbc.Driver";
				//String url		 = "jdbc:mysql://cs3.calstatela.edu:8080/cs437group01";
				String url		 = "jdbc:mysql://cs3.calstatela.edu:3306/cs437group01";
				String dbusername  = "cs437group01";
				String dbpassword  = "VHj#dj!v";
	
				try{
					// Include the driver class
					Class.forName( className );
	
					// Establish a connection to the database
					Connection connection =
							DriverManager.getConnection(url, dbusername, dbpassword);
	
					// Create the query
					String query =	"INSERT INTO Timesheets (EmployeeID, ClockIn, ClockOut) VALUES ( '"+eID+"', '"+cI+"', '"+ClockOut+"')";
	
					// Get a reference to the statement object that will execute our query
					// on the server
					Statement statement = connection.createStatement();
					
					// Execute the query and Store the returned records (ResultSet)
					statement.executeUpdate(query);	
					
					// Close our DB Connection
					connection.close();
	
					session.setAttribute("loggedIn", null);
					session.setAttribute("EmployeeID", null);
					session.setAttribute("username", null);
					session.setAttribute("employee", null);
					
					session.invalidate();
	
					//Clear cookies even if there aren't any
					Cookie userCookie = new Cookie("EmployeeID", "");
					userCookie.setMaxAge(0);
					response.addCookie( userCookie );
	
				}
				catch(SQLException e){		
					e.printStackTrace();
				}
				catch(ClassNotFoundException e){
					e.printStackTrace();
				}
				
				request.setAttribute("closewindow", true);
		
		String pathToView = "/WEB-INF/login/Login.jsp";
		
		RequestDispatcher dispatcher =
				request.getRequestDispatcher( pathToView );
			
			dispatcher.forward(request, response);
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
